package net.wexoo.onlinedivelog.view.pages.content.dive;

import java.util.Iterator;

import net.wexoo.onlinedivelog.entity.dive.DiveBean;
import net.wexoo.onlinedivelog.entity.diver.DiverBean;
import net.wexoo.onlinedivelog.proxy.dive.IDiveProxy;
import net.wexoo.onlinedivelog.reusable.modalwindow.WEXModalWindow;
import net.wexoo.onlinedivelog.reusable.page.WEXWebPage;
import net.wexoo.onlinedivelog.view.pages.detail.dive.DiveDetailPage;
import net.wexoo.onlinedivelog.view.pages.main.MainPage;

import org.apache.log4j.Logger;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class DiveOverview extends MainPage {

  private static final long serialVersionUID = 1L;

  private DiveTablePanel tablePanel;
  private WEXModalWindow<DiveBean> diveWindow;

  private ISortableDataProvider<DiveBean> diveProvider;

  @SpringBean(name = "diveProxy")
  private IDiveProxy diveProxy;

  public DiveOverview() {

    diveProvider = buildDataProvider();

    tablePanel = buildTablePanel(diveProvider);
    add(tablePanel);

    buildDiveWindow();

    buildDiveButton();
  }

  private ISortableDataProvider<DiveBean> buildDataProvider() {
    ISortableDataProvider<DiveBean> diveProvider = new SortableDataProvider<DiveBean>() {
      private static final long serialVersionUID = 1L;

      @Override
      public Iterator<? extends DiveBean> iterator(int first, int count) {
        return diveProxy.getAll(getSort(), first, count).iterator();
      }

      @Override
      public IModel<DiveBean> model(DiveBean object) {
        return new Model<DiveBean>(object);
      }

      @Override
      public int size() {
        return diveProxy.getCount().intValue();
      }
    };
    return diveProvider;
  }


  private DiveTablePanel buildTablePanel(ISortableDataProvider<DiveBean> diveProvider) {
    DiveTablePanel newTablePanel = new DiveTablePanel("diveList", diveProvider) {

      private static final long serialVersionUID = 1L;

      @Override
      protected void openDetailView(DiveBean bean, AjaxRequestTarget target) {
        bean = diveProxy.getBeanById(bean.getId());
        Logger.getLogger(getClass()).debug("Divers of Dive: ");

        for (DiverBean diver : bean.getDivers())
          Logger.getLogger(getClass()).debug("\n - " + diver);

        diveWindow.loadByBean(bean);
        diveWindow.show(target);
      }
    };
    newTablePanel.setOutputMarkupId(true);
    return newTablePanel;
  }

  private void buildDiveWindow() {
    diveWindow = new WEXModalWindow<DiveBean>("diveWindow") {

      private static final long serialVersionUID = 1L;

      @Override
      protected WEXWebPage returnPageToBeCreated(DiveBean bean) {
        if (bean == null)
          bean = new DiveBean();
        return new DiveDetailPage(bean);
      }

      @Override
      protected void onCloseAction(AjaxRequestTarget target) {
        // if (windowContent.dataChanged) -- dataChanged always false
        refreshTablePanel(target, true);
      }
    };
    add(diveWindow);
  }

  protected void refreshTablePanel(AjaxRequestTarget target, boolean daoAccessNeeded) {
    if (daoAccessNeeded)
      diveProvider = buildDataProvider();

    DiveTablePanel newTablePanel = buildTablePanel(diveProvider);

    tablePanel.replaceWith(newTablePanel);
    tablePanel = newTablePanel;
    target.add(tablePanel);
  }

  private void buildDiveButton() {
    AjaxLink<Void> register = new AjaxLink<Void>("diveButton") {

      private static final long serialVersionUID = 1L;

      @Override
      public void onClick(AjaxRequestTarget target) {
        diveWindow.loadWithNewBean();
        diveWindow.show(target);
      }
    };
    add(register);
  }
}
